package com.jlc.book.shop.action;

import java.util.Calendar;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jlc.book.shop.delegate.OrderDelegate;
import com.jlc.book.shop.to.OrderTO;
import com.jlc.book.shop.to.UserTO;
import com.jlc.book.shop.util.GetTotalAmount;
import com.jlc.book.shop.validator.JLCDataValidator;

public class PlaceOrderAction {

	public String placeOrder(HttpServletRequest request,
			HttpServletResponse response) {
		
		String page="placeOrderDef.jsp";
		boolean isError=false;
		
		String address=request.getParameter("address");
		String street=request.getParameter("street");
		String city=request.getParameter("city");
		String state=request.getParameter("state");
		
		String country=request.getParameter("country");
		String zip=request.getParameter("zip");
		String cardNo=request.getParameter("cardNo");
		String pin=request.getParameter("pin");
		String cardExp=request.getParameter("cardExp");
		
		if(!JLCDataValidator.validateRequired(address))
		{
			request.setAttribute("address", "Address is Required");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(address, 3))
		{
			request.setAttribute("address", "Address must be minimum 3 character");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(address, 25))
		{
			request.setAttribute("address", "Address must be maximum 25 character");
			isError=true;
		}
		
		
		if(!JLCDataValidator.validateRequired(street))
		{
			request.setAttribute("street", "Street is Required");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(street, 3))
		{
			request.setAttribute("street", "Street must be minimum 3 character");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(street,30))
		{
			request.setAttribute("street", "Street must be maximum 30 character");
			isError=true;
		}
		

		if(!JLCDataValidator.validateRequired(city))
		{
			request.setAttribute("city", "City is Required");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(street, 3))
		{
			request.setAttribute("city", "City must be minimum 3 character");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(street,30))
		{
			request.setAttribute("city", "City must be maximum 30 character");
			isError=true;
		}
		
		if(!JLCDataValidator.validateRequired(state))
		{
			request.setAttribute("state", "State is Required");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(state, 2))
		{
			request.setAttribute("state", "State must be minimum 2 character");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(state,20))
		{
			request.setAttribute("state", "State must be maximum 20 character");
			isError=true;
		}
		

		if(!JLCDataValidator.validateRequired(country))
		{
			request.setAttribute("country", "Country is Required");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(country, 2))
		{
			request.setAttribute("country", "Country must be minimum 2 character");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(country,20))
		{
			request.setAttribute("country", "Country must be maximum 20 character");
			isError=true;
		}
		
		

		if(!JLCDataValidator.validateRequired(zip))
		{
			request.setAttribute("zip", "Zip is Required");
			isError=true;
		}
		else if(!JLCDataValidator.validateLong(zip))
		{
			request.setAttribute("zip", "Zip must be digits only");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(zip, 6))
		{
			request.setAttribute("zip", "Zip must be minimum 6 digits ");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(zip,6))
		{
			request.setAttribute("zip", "Zip must be maximum 6 digits");
			isError=true;
		}
		

		if(!JLCDataValidator.validateRequired(cardNo))
		{
			request.setAttribute("cardNo", "Card No is Required");
			isError=true;
		}
		else if(!JLCDataValidator.validateLong(cardNo))
		{
			request.setAttribute("cardNo", "Card No must be digits only");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(cardNo, 16))
		{
			request.setAttribute("cardNo", "Card No must be minimum 16 digits ");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(cardNo,16))
		{
			request.setAttribute("cardNo", "Card No must be maximum 16 digits");
			isError=true;
		}
		
		

		if(!JLCDataValidator.validateRequired(pin))
		{
			request.setAttribute("pin", "Secret pin is Required");
			isError=true;
		}
		else if(!JLCDataValidator.validateLong(pin))
		{
			request.setAttribute("pin", " Secret pin must be digits only");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(pin, 4))
		{
			request.setAttribute("pin", "Secret pin must be minimum 4 digits ");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(pin,4))
		{
			request.setAttribute("pin", "Secret pin must be maximum 4 digits");
			isError=true;
		}
		
		

		if(!JLCDataValidator.validateRequired(cardExp))
		{
			request.setAttribute("cardExp", "Expiry date is Required");
			isError=true;
		}
		else if(!JLCDataValidator.validateExpDate(cardExp))
		{
			request.setAttribute("cardExp", "Not a valid expiry date");
			isError=true;
		}
		else if(!JLCDataValidator.minLength(cardExp, 6))
		{
			request.setAttribute("cardExp", "Not a min length ");
			isError=true;
		}
		else if(!JLCDataValidator.maxLength(cardExp,7))
		{
			request.setAttribute("cardExp", "Not a max length");
			isError=true;
		}
		
		
		if(!isError)
		{
			String orderDate=Calendar.getInstance().get(Calendar.DATE)+"/"+
			(Calendar.getInstance().get(Calendar.MONTH)+1)+"/"+
			Calendar.getInstance().get(Calendar.YEAR);
			
	Set orderItemList=(Set) request.getSession().getAttribute("SELECTED_BOOK_LIST");
			
			float totalAmount=0.0f;
			
			int totalItem=0;
			
			Object obj=request.getSession().getAttribute("TOTAL_BOOK_QUANTITY");
			
			if(obj!=null)
			{
				Integer i=(Integer) obj;
				totalItem=i.intValue();
			}
			
			obj=request.getSession().getAttribute("TOTAL_BOOK_AMOUNT");
			if(obj!=null)
			{
				Double d=(Double) obj;
				totalAmount=Float.parseFloat(GetTotalAmount.geTotalAmount(d.toString()));
			}
			
			UserTO userTo=(UserTO) request.getSession().getAttribute("USER_TO");
			
			OrderTO oto=new OrderTO();
			oto.setAddress(address);
			oto.setCardNo(cardNo);
			oto.setCity(city);
			oto.setCountry(country);
			oto.setUserId(userTo.getUserId());
			oto.setExpDate(cardExp);
			oto.setOrderDate(orderDate);
			oto.setOrderItemList(orderItemList);
			oto.setState(state);
			oto.setStreet(street);
			oto.setTotalAmount(totalAmount);
			oto.setTotalItem(totalItem);
			oto.setZip(zip);
			
			String orderId=OrderDelegate.placeOrder(oto,request.getRemoteAddr());
			
			if(orderId!=null)
			{
				request.setAttribute("ORDER_PLACED", orderId);
				
				HttpSession session=request.getSession();
				session.removeAttribute("TOTAL_BOOK_AMOUNT");
				session.removeAttribute("TOTAL_BOOK_QUANTITY");
			}
			else
			{
				request.setAttribute("errorInOrder", "Error occured while placing order.Please try later");
			}
			
		}
		
		return page;
	}

}
